# -*-coding:utf-8 -*-
from xadmin.sites import site
from django.dispatch import receiver
from django.contrib.auth.signals import user_logged_in, user_logged_out
from xadmin.views import BaseAdminPlugin, CommAdminView, ListAdminView
from django.forms.widgets import Media
from org.models import Department
from django.template import loader
from django.conf import settings
import json

top_department_id = getattr(settings, 'LOCAL_TOP_DEPARTMENT_ID', None)

CURRENT_POLICE = 'current_person'
USR_DEP = 'user_department'
USR_DEPS = 'users_departmnets'
USR_PDEPS = 'users_parent_departmnets'
@receiver(user_logged_in)
def login_callback(sender,request,user,**kwargs):
    if hasattr(user, 'person') and user.person:
        #request.session[CURRENT_POLICE] = user.person
        #request.session[USR_DEP] = user.person.ssbm
        request.session[USR_DEPS] = Department.objects.children_ids(user.person.ssbm.id)
        request.session[USR_PDEPS] = Department.objects.parents_ids(user.person.ssbm.id)
        pass

@receiver(user_logged_out)
def logout_callback(sender,request,user,**kwargs):
    if hasattr(user, 'person') and user.person:
        pass

class OrgPlugin(BaseAdminPlugin):
    def init_request(self, *args, **kwargs):
        #session = self.admin_view.request.session
        ##self.person = session.get(CURRENT_POLICE,None)
        #self.user_department = session.get(USR_DEP,None)
        self.admin_view.children_depids = self.request.session.get(USR_DEPS,[])
        self.admin_view.parent_depids = self.request.session.get(USR_PDEPS,[])
        try:
            self.admin_view.person = self.admin_view.request.user.person
        except:
            self.admin_view.person = None
        try:
            self.admin_view.department = self.admin_view.person.department
        except:
            self.admin_view.department = None
        return True
site.register_plugin(OrgPlugin, CommAdminView)


class DepartmentTreePlugin(BaseAdminPlugin):

    department_tree_field = None

    def init_request(self, *args, **kwargs):
        return self.department_tree_field is not None

    def block_nav_menu(self, context, nodes):
        dep_id = self.request.GET.get('_rel_%s__id__exact' % self.department_tree_field, top_department_id)
        try:
            c_dep = Department.objects.get(id=dep_id)
            p_dep = c_dep.sjbm
        except:
            # 读取顶级部门
            try:
                p_dep = Department.objects.filter(sjbm=None).get()
            except:
                p_dep = None
        html = loader.render_to_string(
            'org/xadmin/blocks/model_list.nav_menu.deptree.html',
            {
                'dep_id': p_dep.id if p_dep else None,
                'c_id': dep_id,
                'dep_field': self.department_tree_field
            })
        nodes.insert(0, html)

    def get_media(self, media):
        new = Media(js=['org/xadmin/selectdepartment.js'])
        return media + new


# site.register_plugin(DepartmentTreePlugin, ListAdminView)
